#coding=utf-8
import unittest
import os
import sys
import time
import datetime
import shutil

base_dir = 'd:/sql/cgdb/'
sql_bak_path = 'd:/sql/bak/'

ip_address = '192.168.32.200'
port = '3306'
user_name = 'root'
password = 'egova'
datebases_name = 'hzcg1101'

class mySqlImport():

    def import_sqls(self):

        connectInfor = 'mysql -h 192.168.32.200 -P 3306 -uroot -pegova'
        db_info = ip_address + ' -P ' + port + ' -u' + user_name + ' -p' +  password +' ' + datebases_name
        base_command = 'mysql -h '
        databases = 'hzcg1101'
        sql_size_all = 0
        sql_file_list = os.listdir(base_dir)
        StartCycle = datetime.datetime.now()
        for i in range(0,len(sql_file_list)):
            sql_file = base_dir + sql_file_list[i]
            sql_file_bak = sql_bak_path + sql_file_list[i]
            command = connectInfor + ' '+ databases + ' < ' + sql_file
            command2 = base_command + db_info + ' < ' + sql_file + ' --show-warnings=false -f'
            sql_size = os.path.getsize(sql_file) / 1024 / 1024
            sql_size_all = sql_size_all + sql_size
            print u'开始导入sql文件:'+ sql_file
            start = datetime.datetime.now()
            os.system(command2)
            end = datetime.datetime.now()
            useTime = (end-start).seconds
            shutil.move(sql_file,sql_file_bak)
            print 'sqlname:'+sql_file_list[i]+'; size= ' + str(sql_size) + 'M; useTime=' + str(useTime) + 's '

        endCycle = datetime.datetime.now()
        use_time_all = (endCycle - StartCycle).seconds
        print 'AllFileSize= ' + str(sql_size_all) + 'M; useTime=' + str(use_time_all) + 's '
        return 0

    def floder_operate(self):
        global sql_bak_path,base_dir
        if os.path.exists(base_dir):
            if base_dir.endswith('/'):
                sql_bak_path = base_dir[0:len(base_dir)-1] + '_bak/'
            else:
                sql_bak_path = base_dir+'_bak/'
                base_dir = base_dir + '/'
            if os.path.exists(sql_bak_path):
                shutil.rmtree(sql_bak_path)
            os.mkdir(sql_bak_path)
        else:
            exit()

    def get_db_info(self):
        global ip_address,datebases_name,user_name,password,base_dir
        print u'请输入导入的服务器ip地址:'
        ip_address = raw_input()
        print u'请输入导入的数据库实例名称：'
        datebases_name = raw_input()
        print  u'请输入连接数据库的用户名：'
        user_name = raw_input()
        print  u'请输入连接数据库的密码：'
        password = raw_input()
        print  u'请输入SQL文件的路径：'
        base_dir = raw_input()
        print ip_address + ' ' + datebases_name + ' ' + user_name + ' ' + password + ' ' + base_dir

if __name__ == '__main__':
    dbImportInstance = mySqlImport()
    dbImportInstance.get_db_info()
    print u'请确认信息是否正确，正确输入Y 进行数据库导出'
    makeSure = raw_input()
    if makeSure == 'Y':
        dbImportInstance.floder_operate()
        dbImportInstance.import_sqls()
    else:
        print u'退出了1。。。。。'
        exit()
    unittest.main()
